/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package servlets;

import AsocUsu.UnidadFamiliar;
import AsocUsu.Usuario;
import baseDatos.ConsultasBD;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author oracle
 */
public class ConsUsuYFam extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        ConsultasBD baseDatos = new ConsultasBD();
        HttpSession sesion = request.getSession();

        String usuario = (String) sesion.getAttribute("cif");

        String dni = request.getParameter("buscar");
        System.out.println(dni);
        Usuario u = baseDatos.consultarUsuario(dni.toUpperCase());
        
        if (u != null){
            List<UnidadFamiliar> listaFam = baseDatos.listarFamUsuario(u.nExpediente());
            sesion.setAttribute("nExp", u.nExpediente());
            sesion.setAttribute("nombre",u.nombre());
            sesion.setAttribute("apellidos",u.apellidos());
            sesion.setAttribute("dni",u.dni());
            sesion.setAttribute("tarjResi",(u.tarjResidente() == null)? "": u.tarjResidente());
            sesion.setAttribute("nacionalidad",(u.nacionalidad() == null)? "": u.nacionalidad());
            sesion.setAttribute("fechaNac",u.fechaNac());
            sesion.setAttribute("tipoVia",u.tipoVia());
            sesion.setAttribute("nombreVia",u.nombreVia());
            sesion.setAttribute("numeroVia",u.numeroVia());
            sesion.setAttribute("ciudad",(u.ciudad() == null)? "": u.ciudad());
            sesion.setAttribute("provincia",(u.provincia() == null)? "": u.provincia());
            sesion.setAttribute("cp",u.cP());
            sesion.setAttribute("asoc", u.asociacion());
            sesion.setAttribute("nExpConsulta", u.nExpediente());
            sesion.setAttribute("tipoUsu", "cabeza");

            if (usuario.equalsIgnoreCase("admin") || usuario.equalsIgnoreCase(u.asociacion())){
                sesion.setAttribute("tamFam", listaFam.size());
                if (listaFam.size() > 0){
                    for (UnidadFamiliar uf : listaFam){
                        sesion.setAttribute("nExp" + listaFam.indexOf(uf), uf.nExpediente());
                        sesion.setAttribute("identificativo" + listaFam.indexOf(uf), uf.identificativo());
                        sesion.setAttribute("parentesco" + listaFam.indexOf(uf), uf.parentesco());
                        sesion.setAttribute("nombre" + listaFam.indexOf(uf), uf.nombre());
                        sesion.setAttribute("apellidos" + listaFam.indexOf(uf), uf.apellidos());
                        sesion.setAttribute("dni" + listaFam.indexOf(uf), uf.dni());
                    }
                }

                response.sendRedirect("../consUsuYFam.jsp");
            } else {
                sesion.setAttribute("error", "El usuario no pertenece a su asociacion");
                response.sendRedirect("../error.jsp");
            }
        }else {
            UnidadFamiliar uf = baseDatos.consultarUnidadFamiliar(dni.toUpperCase());
            if (uf != null){
                sesion.setAttribute("nExp", uf.nExpediente());
                sesion.setAttribute("identificativo", uf.identificativo());
                sesion.setAttribute("parentesco", uf.parentesco());
                sesion.setAttribute("nombre", uf.nombre());
                sesion.setAttribute("apellidos", uf.apellidos());
                sesion.setAttribute("dni", uf.dni());
                sesion.setAttribute("tipoUsu", "fam");
                response.sendRedirect("../consUsuYFam.jsp");
            }else
                response.sendRedirect("../noenc.jsp");
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
